package org.marsdonne.auth;

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;

@EnableJms
@Configuration
@ConditionalOnProperty(prefix = "auth", name = "artemis.broker-url")
public class AuthConfigurer {
    @Value("${auth.artemis.broker-url:tcp://localhost:61616}")
    private String brokerUrl;
    @Value("${auth.artemis.username:admin}")
    private String username;
    @Value("${auth.artemis.password:admin}")
    private String password;

    @Bean
    public ConnectionFactory authJmsConnectionFactory() {
        return new ActiveMQConnectionFactory(brokerUrl, username, password);
    }

    @Bean
    public JmsTemplate authJmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(authJmsConnectionFactory());
        return jmsTemplate;
    }

    @Bean
    public UserServ userServ() {
        return new UserServImpl();
    }
}
